package com.est.android.wordtime;

import com.est.android.wordtime.R;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Bundle;
import android.text.format.Time;
import android.widget.RemoteViews;

public class WordTimeWidget extends AppWidgetProvider {
	
	public static final int NUM_OF_CLOCK_IMAGES = 25;
	public static final int CLOCK_UPDATE_RATE = 10000; // Updates every 10 seconds
	public static final String WORDTIME_UPDATE_INTENT = "WORDTIME_WIDGET_UPDATE";
	public static final int WORDTIME_TOP = 0;
	public static final int WORDTIME_LEFT = 0;
	public static final int WORDTIME_WIDTH = 320;
	public static final int WORDTIME_HEIGHT = 320;
	
	// Image and on/off flag indices
	public static int WORDTIME_IMG_BACKGROUND	= 0;
	public static int WORDTIME_IMG_OCLOCK 		= 1;
	public static int WORDTIME_IMG_TO 			= 2;
	public static int WORDTIME_IMG_IT 			= 3; 
	public static int WORDTIME_IMG_IS 			= 4;
	public static int WORDTIME_IMG_A 			= 5;
	public static int WORDTIME_IMG_TIME 		= 6;
	public static int WORDTIME_IMG_QUARTER 		= 7;
	public static int WORDTIME_IMG_HALF 		= 8;
	public static int WORDTIME_IMG_PAST 		= 9;
	public static int WORDTIME_IMG_MINUTE_5 	= 10;
	public static int WORDTIME_IMG_MINUTE_10 	= 11;
	public static int WORDTIME_IMG_MINUTE_20 	= 12;
	public static int WORDTIME_IMG_HOUR_1 		= 13;
	public static int WORDTIME_IMG_HOUR_2 		= 14;
	public static int WORDTIME_IMG_HOUR_3 		= 15;
	public static int WORDTIME_IMG_HOUR_4 		= 16;
	public static int WORDTIME_IMG_HOUR_5 		= 17;
	public static int WORDTIME_IMG_HOUR_6 		= 18;
	public static int WORDTIME_IMG_HOUR_7 		= 19;
	public static int WORDTIME_IMG_HOUR_8 		= 20;
	public static int WORDTIME_IMG_HOUR_9 		= 21;
	public static int WORDTIME_IMG_HOUR_10 		= 22;
	public static int WORDTIME_IMG_HOUR_11 		= 23;
	public static int WORDTIME_IMG_HOUR_12 		= 24;
	
	static AlarmManager sAlarmManager;
	static PendingIntent sPendingIntent;
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		
		for(int i = 0; i < appWidgetIds.length; i++) {
			int curAppWidgetId = appWidgetIds[i];
			updateWordTimeWidget(context, appWidgetManager, curAppWidgetId);
		}
	}

	public static void updateWordTimeWidget(
			Context context,
			AppWidgetManager appWidgetManager,
			int curAppWidgetId) {
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.main);
		
		views.setImageViewBitmap(R.id.ImgClock, updateWordTime(context));
		appWidgetManager.updateAppWidget(curAppWidgetId, views);
	}
	
	@Override
	public void onDisabled(Context context) {
		super.onDisabled(context);
		
		sAlarmManager.cancel(sPendingIntent);
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		super.onReceive(context, intent);
		
		if(WORDTIME_UPDATE_INTENT.equals(intent.getAction()))
		{
			Bundle extras = intent.getExtras();
			
			if(extras != null) {
				AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
				ComponentName thisAppWidget = new ComponentName(context.getPackageName(), WordTimeWidget.class.getName());
				int[] appWidgetIds = appWidgetManager.getAppWidgetIds(thisAppWidget);
				
				onUpdate(context, appWidgetManager, appWidgetIds);
			}
		}
	}
	
	private static Drawable[] getUpdatedWordTimeImages(Context context, boolean[] imgStatuses) {
		Resources r = context.getResources();
		Drawable[] results = new Drawable[NUM_OF_CLOCK_IMAGES];
		
		// Background goes first since images are overlaid lowest to highest.
		results[WORDTIME_IMG_BACKGROUND] = r.getDrawable(R.drawable.background);

		results[WORDTIME_IMG_OCLOCK] = imgStatuses[WORDTIME_IMG_OCLOCK] ? r.getDrawable(R.drawable.oclock_white) : r.getDrawable(R.drawable.oclock_grey);
		results[WORDTIME_IMG_TO] = imgStatuses[WORDTIME_IMG_TO] ? r.getDrawable(R.drawable.to_white) : r.getDrawable(R.drawable.to_grey);
		results[WORDTIME_IMG_IT] = imgStatuses[WORDTIME_IMG_IT] ? r.getDrawable(R.drawable.it_white) : r.getDrawable(R.drawable.it_grey);
		results[WORDTIME_IMG_IS] = imgStatuses[WORDTIME_IMG_IS] ? r.getDrawable(R.drawable.is_white) : r.getDrawable(R.drawable.is_grey);
		results[WORDTIME_IMG_A] = imgStatuses[WORDTIME_IMG_A] ? r.getDrawable(R.drawable.a_white) : r.getDrawable(R.drawable.a_grey);
		results[WORDTIME_IMG_TIME] = imgStatuses[WORDTIME_IMG_TIME] ? r.getDrawable(R.drawable.time_white) : r.getDrawable(R.drawable.time_grey);
		results[WORDTIME_IMG_QUARTER] = imgStatuses[WORDTIME_IMG_QUARTER] ? r.getDrawable(R.drawable.quarter_white) : r.getDrawable(R.drawable.quarter_grey);
		results[WORDTIME_IMG_HALF] = imgStatuses[WORDTIME_IMG_HALF] ? r.getDrawable(R.drawable.half_white) : r.getDrawable(R.drawable.half_grey);
		results[WORDTIME_IMG_PAST] = imgStatuses[WORDTIME_IMG_PAST] ? r.getDrawable(R.drawable.past_white) : r.getDrawable(R.drawable.past_grey);
		results[WORDTIME_IMG_MINUTE_5] = imgStatuses[WORDTIME_IMG_MINUTE_5] ? r.getDrawable(R.drawable.minute_5_white) : r.getDrawable(R.drawable.minute_5_grey);
		results[WORDTIME_IMG_MINUTE_10] = imgStatuses[WORDTIME_IMG_MINUTE_10] ? r.getDrawable(R.drawable.minute_10_white) : r.getDrawable(R.drawable.minute_10_grey);
		results[WORDTIME_IMG_MINUTE_20] = imgStatuses[WORDTIME_IMG_MINUTE_20] ? r.getDrawable(R.drawable.minute_20_white) : r.getDrawable(R.drawable.minute_20_grey);
		results[WORDTIME_IMG_HOUR_1] = imgStatuses[WORDTIME_IMG_HOUR_1] ? r.getDrawable(R.drawable.hour_1_white) : r.getDrawable(R.drawable.hour_1_grey);
		results[WORDTIME_IMG_HOUR_2] = imgStatuses[WORDTIME_IMG_HOUR_2] ? r.getDrawable(R.drawable.hour_2_white) : r.getDrawable(R.drawable.hour_2_grey);
		results[WORDTIME_IMG_HOUR_3] = imgStatuses[WORDTIME_IMG_HOUR_3] ? r.getDrawable(R.drawable.hour_3_white) : r.getDrawable(R.drawable.hour_3_grey);
		results[WORDTIME_IMG_HOUR_4] = imgStatuses[WORDTIME_IMG_HOUR_4] ? r.getDrawable(R.drawable.hour_4_white) : r.getDrawable(R.drawable.hour_4_grey);
		results[WORDTIME_IMG_HOUR_5] = imgStatuses[WORDTIME_IMG_HOUR_5] ? r.getDrawable(R.drawable.hour_5_white) : r.getDrawable(R.drawable.hour_5_grey);
		results[WORDTIME_IMG_HOUR_6] = imgStatuses[WORDTIME_IMG_HOUR_6] ? r.getDrawable(R.drawable.hour_6_white) : r.getDrawable(R.drawable.hour_6_grey);
		results[WORDTIME_IMG_HOUR_7] = imgStatuses[WORDTIME_IMG_HOUR_7] ? r.getDrawable(R.drawable.hour_7_white) : r.getDrawable(R.drawable.hour_7_grey);
		results[WORDTIME_IMG_HOUR_8] = imgStatuses[WORDTIME_IMG_HOUR_8] ? r.getDrawable(R.drawable.hour_8_white) : r.getDrawable(R.drawable.hour_8_grey);
		results[WORDTIME_IMG_HOUR_9] = imgStatuses[WORDTIME_IMG_HOUR_9] ? r.getDrawable(R.drawable.hour_9_white) : r.getDrawable(R.drawable.hour_9_grey);
		results[WORDTIME_IMG_HOUR_10] = imgStatuses[WORDTIME_IMG_HOUR_10] ? r.getDrawable(R.drawable.hour_10_white) : r.getDrawable(R.drawable.hour_10_grey);
		results[WORDTIME_IMG_HOUR_11] = imgStatuses[WORDTIME_IMG_HOUR_11] ? r.getDrawable(R.drawable.hour_11_white) : r.getDrawable(R.drawable.hour_11_grey);
		results[WORDTIME_IMG_HOUR_12] = imgStatuses[WORDTIME_IMG_HOUR_12] ? r.getDrawable(R.drawable.hour_12_white) : r.getDrawable(R.drawable.hour_12_grey);
		
		return results;
	}
	
	private static Bitmap updateWordTime(Context context) {
		Bitmap result;
		Time time = new Time();
		
		time.setToNow();
		
		boolean[] imgStatuses = new boolean[NUM_OF_CLOCK_IMAGES];
		
		for(int i = 0; i < NUM_OF_CLOCK_IMAGES; i++) {
			imgStatuses[i] = false;
		}
		
		int hour = time.hour;
		int minute = time.minute;
		
		minute = minute / 5; // always round down
		
		imgStatuses[WORDTIME_IMG_IT] = true;
		imgStatuses[WORDTIME_IMG_IS] = true;
		imgStatuses[WORDTIME_IMG_OCLOCK] = true;
		
		if(minute > 6) {
			imgStatuses[WORDTIME_IMG_TO] = true;
			hour++;
		}
		else if (minute != 0){
			imgStatuses[WORDTIME_IMG_PAST] = true;
		}
		
		// Set the minute images
		switch(minute) {
		case 1:
		case 11:
			imgStatuses[WORDTIME_IMG_MINUTE_5] = true;
			break;
		case 2:
		case 10:
			imgStatuses[WORDTIME_IMG_MINUTE_10] = true;
			break;
		case 3:
		case 9:
			imgStatuses[WORDTIME_IMG_A] = true;
			imgStatuses[WORDTIME_IMG_QUARTER] = true;
			break;
		case 4:
		case 8:
			imgStatuses[WORDTIME_IMG_MINUTE_20] = true;
			break;
		case 5:
		case 7:
			imgStatuses[WORDTIME_IMG_MINUTE_20] = true;
			imgStatuses[WORDTIME_IMG_MINUTE_5] = true;
			break;
		case 6:
			imgStatuses[WORDTIME_IMG_HALF] = true;
			break;
		}
		
		// Set the hour image
		switch(hour) {
		case 1: 
		case 13:
			imgStatuses[WORDTIME_IMG_HOUR_1] = true;
			break;
		case 2: 
		case 14:
			imgStatuses[WORDTIME_IMG_HOUR_2] = true;
			break;
		case 3: 
		case 15:
			imgStatuses[WORDTIME_IMG_HOUR_3] = true;
			break;
		case 4: 
		case 16:
			imgStatuses[WORDTIME_IMG_HOUR_4] = true;
			break;
		case 5:
		case 17:
			imgStatuses[WORDTIME_IMG_HOUR_5] = true;
			break;
		case 6: 
		case 18:
			imgStatuses[WORDTIME_IMG_HOUR_6] = true;
			break;
		case 7: 
		case 19:
			imgStatuses[WORDTIME_IMG_HOUR_7] = true;
			break;
		case 8: 
		case 20:
			imgStatuses[WORDTIME_IMG_HOUR_8] = true;
			break;
		case 9: 
		case 21:
			imgStatuses[WORDTIME_IMG_HOUR_9] = true;
			break;
		case 10:
		case 22: 
			imgStatuses[WORDTIME_IMG_HOUR_10] = true;
			break;
		case 11:
		case 23:
			imgStatuses[WORDTIME_IMG_HOUR_11] = true;
			break;
		case 12:
		case 0:
			imgStatuses[WORDTIME_IMG_HOUR_12] = true;
			break;
		}
		
		Canvas canvas;
		LayerDrawable layers;
				
		layers = new LayerDrawable(getUpdatedWordTimeImages(context, imgStatuses));
		layers.setBounds(WORDTIME_LEFT, WORDTIME_TOP, WORDTIME_WIDTH, WORDTIME_HEIGHT);
		
		result = Bitmap.createBitmap(WORDTIME_WIDTH, WORDTIME_HEIGHT, Config.ARGB_8888);
		canvas = new Canvas(result);
		layers.draw(canvas);
		
		return result;
	}

	
	public static void SaveAlarmManager(AlarmManager alarmManager,
			PendingIntent pendingIntent) {
		sAlarmManager = alarmManager;
		sPendingIntent = pendingIntent;
	}
}